home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9923 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  89 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: HELP! Modifying the EOF in a file!
  5. Message-ID: <1996Mar14.122030.9220@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <4i585k$4ia@news.netam.net> <4i5pb4$fae@sparcserver.lrz-muenchen.de>
  11. Date: Thu, 14 Mar 1996 12:19:52 GMT
  12.  
  13. ua302aa@lrz-muenchen.de () wrote:
  14.  
  15. >bgc@alpha.netam.net (The Bowling Green Connection) writes:
  16.  
  17. >>I am having trouble shortening the size of a text file...
  18. >>Suppose I have a text file with 3 lines of data, then I
  19. >>run this program:
  20.  
  21. >>void main() {
  22.  
  23. >Next to no comment :-)
  24.  
  25. Okay, I'll say it:  No one should declare void main() and fail to
  26. return a valid result code to the execution environment, no matter how
  27. successfully the program compiles or runs.
  28.  
  29. >>   FILE *dat;
  30. >>   dat=fopen("file.txt", "r+");
  31.  
  32. >I fail to see where you want to _write_ to "file.txt"
  33.  
  34. Look up "r+" mode and see where it says the file is opened for both
  35. reading and writing.
  36.  
  37. >>   fprintf(dat, "Hello! %c", EOF);
  38.  
  39. >EOF cannot be a char, so printing it as a char might be a
  40. >bad idea.
  41.  
  42. True.
  43.  
  44. The C standard I/O library doesn't support this kind of thing.  You
  45. can't just write EOF into a file like that.  EOF is given not to be
  46. ANY possible character value, and the format specifier %c can't write
  47. anything other than a character value.  Even if you used putc instead
  48. (which takes an int which could conceivably represent the value of EOF
  49. distinct from any char) it still won't work because the standard
  50. didn't say it could.  In some file systems it's just not possible to
  51. take an existing file and change it's length, and so to be portable to
  52. those platforms, the Standard adopted this limitation.
  53.  
  54. Therefore, according to the Standard, it's just not possible to do
  55. this.  And if you believe that the only C programs are Standard C
  56. programs, you'd better stop reading.
  57.  
  58. In MSDOS (and OS/2 and Windows and Windows NT), this works if you
  59. write the character value 26 (decimal, or '\032') instead of EOF.
  60. MSDOS text files' format harken back to the days of yore, of version
  61. 1.0, in the legacy of RT-11 and CP/M, when the file system couldn't
  62. tell the length of a file any more accurately that the number of
  63. sectors that contained it.  Therefore CP/M put a character value in
  64. the data stream to show EOF for text files, so they wouldn't always
  65. have to have up to 511 extra characters at the end, and that character
  66. value is 26, Control-Z.  Nowadays the MSDOS file system knows how long
  67. files are to the byte and the 26 isn't needed, but for compatability
  68. it is still interpreted that way.
  69.  
  70. (RT-11's solution is a bit different.  RT-11 text files are
  71. interpreted as not containing any 0 characters, they may be in fact be
  72. interspersed in the text.  Typically the last sector of an RT-11 text
  73. file is filled with 0s.)
  74.  
  75. Note however that if you do this the file doesn't really get shorter.
  76. It takes the same amount of disk space, and programs that read the
  77. file in binary ("rb") can read your '\032', and right past into the
  78. remaining data, until the real EOF.
  79.  
  80. Also note that this will not work with unix or unix-like systems.  26
  81. is just a character value with no special significance.  Most provide
  82. some other special IO function to shorten a file.  It might be called
  83. chsize().
  84.  
  85. --
  86. Richard Krehbiel, Kastle Systems, Arlington VA USA
  87. rich@kastle.com (work) or richk@mnsinc.com (personal)
  88.  
  89.